﻿// LoginDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "LoginDlg.h"
#include "afxdialogex.h"


// CLoginDlg 对话框

IMPLEMENT_DYNAMIC(CLoginDlg, CDialogEx)

CLoginDlg::CLoginDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent)
	, m_cPassward(_T(""))
	, m_cNewPassword(_T(""))
{

}

CLoginDlg::~CLoginDlg()
{
}

void CLoginDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_USERNAME, m_cUserName);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_cPassward);
	DDX_Control(pDX, IDC_BUTTON_RESETUSER, buttonResetUser);
}


BEGIN_MESSAGE_MAP(CLoginDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_LOGIN, &CLoginDlg::OnBnClickedButtonLogin)
	ON_CBN_SELCHANGE(IDC_COMBO_MODE_CHOOSE, &CLoginDlg::OnCbnSelchangeComboModeChoose)
	ON_BN_CLICKED(IDC_BUTTON_QUIT, &CLoginDlg::OnBnClickedButtonQuit)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_RESETUSER, &CLoginDlg::OnBnClickedButtonResetuser)
END_MESSAGE_MAP()


// CLoginDlg 消息处理程序

BOOL CLoginDlg::OnInitDialog()
{
	CComboBox* combo_box = nullptr;
	combo_box = (CComboBox*)GetDlgItem(IDC_COMBO_MODE_CHOOSE);

	combo_box->InsertString(0, L"员工");
	combo_box->InsertString(1, L"管理员");
	combo_box->SetCurSel(0);

	ReadLoginConfig();

	return TRUE;
}

void CLoginDlg::ReadLoginConfig()
{
	m_cPasswordCorrect = gSystemMgr.m_sLoginInfo.m_cPassword; 
	m_cUserNameCorrect = gSystemMgr.m_sLoginInfo.m_cUserName;
	m_cUserNameCorrect_Worker = gSystemMgr.m_sLoginInfo.m_cUserName_Worker;
}

void CLoginDlg::OnBnClickedButtonLogin()
{
	UpdateData(TRUE);

	CComboBox* combo_box = (CComboBox*)(GetDlgItem(IDC_COMBO_MODE_CHOOSE));
	auto cboSelcect = combo_box->GetCurSel();

	//if(m_cPasswordCorrect == L"")
	//{
	//	m_cPasswordCorrect = L"1";
	//}

	if(cboSelcect == 0)
	{
		if(m_cUserName == m_cUserNameCorrect_Worker)
		{
			gSystemMgr.isAdmin = false;
			int i = _ttoi(m_cUserName);
			CString cstr;
			cstr.Format(L"员工号:%d  员工登陆成功!", i);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			OnClose();
			EnableControl(TRUE); 
			gSystemMgr.LoginFlag = 0;
			gSystemMgr.UserMsg.Format(L"%d", i);
		}
		else
		{
			gSystemLog.DisplayLogAndSave(L"请输入正确员工号!", LV_ERROR);
		}
		
	}
	if(cboSelcect == 1)
	{
		UpdateData(TRUE);

		if (m_cUserName == m_cUserNameCorrect /*!= L""*/ && m_cPassward == m_cPasswordCorrect)
		{
			gSystemMgr.isAdmin = true;
			ModeFlag = 1;
			int i = _ttoi(m_cUserName);
			CString cstr;
			cstr.Format(L"管理员号:%d  管理员登陆成功!", i);
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			/*OnClose();*/
			//EnableControl(TRUE);
			gSystemMgr.LoginFlag = 1;
			gSystemMgr.UserMsg.Format(L"%d", i);
			MessageBox(cstr, L"提示", MB_ICONINFORMATION);
		}
		else
		{
			gSystemLog.DisplayLogAndSave(L"登陆失败!", LV_ERROR);
		}
		
	}

	m_cUserName = L"";
	m_cPassward = L"";
	UpdateData(FALSE);
}


void CLoginDlg::EnableControl(const BOOL nflag)
{
	/*CButton* btn = nullptr;
	btn = (CButton*)GetDlgItem(IDC_BUTTON_QUIT);
	btn->EnableWindow(nflag);

	CButton* btnLogin = nullptr;
	btnLogin = (CButton*)GetDlgItem(IDC_BUTTON_LOGIN);
	btnLogin->EnableWindow(!nflag);

	CMenu* pMenu = GetMenu();
	pMenu = AfxGetMainWnd()->GetMenu();*/

	//if (ModeFlag == 0)
	//{
	//	if (nflag == 1)
	//	{
	//		pMenu->EnableMenuItem(ID_32776, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32777, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32775, MF_ENABLED);
	//	}
	//	else
	//	{
	//		pMenu->EnableMenuItem(ID_32781, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32774, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32773, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32778, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32782, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32776, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32777, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32775, MF_DISABLED);
	//	}
	//}
	//if (ModeFlag == 1)
	//{
	//	//CStatic* staticAdmin = (CStatic*)GetDlgItem(IDC_STATIC_NEW_PASSWARD);
	//	//CEdit* editAdmin = (CEdit*)GetDlgItem(IDC_EDIT_NEW_PASSWORD);
	//	//CEdit* btnAdmin = (CEdit*)GetDlgItem(IDC_BUTTON_NEW_PASSWARD);

	//	//staticAdmin->ShowWindow(nflag);
	//	//editAdmin->ShowWindow(nflag);
	//	//btnAdmin->ShowWindow(nflag);


	//	if (nflag == 1)
	//	{
	//		pMenu->EnableMenuItem(ID_32781, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32774, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32773, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32778, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32782, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32776, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32777, MF_ENABLED);
	//		pMenu->EnableMenuItem(ID_32775, MF_ENABLED);
	//	}
	//	else
	//	{
	//		pMenu->EnableMenuItem(ID_32781, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32774, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32773, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32778, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32782, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32776, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32777, MF_DISABLED);
	//		pMenu->EnableMenuItem(ID_32775, MF_DISABLED);
	//	}
	//}
}

void CLoginDlg::OnBnClickedButtonQuit()
{
	gSystemLog.DisplayLogAndSave(L"退出登录成功!", LV_EVENT);
	EnableControl(FALSE);
	ModeFlag = 0;
	gSystemMgr.isAdmin = false;
}



void CLoginDlg::OnCbnSelchangeComboModeChoose()
{
	CComboBox* combo_box = (CComboBox*)(GetDlgItem(IDC_COMBO_MODE_CHOOSE));
	auto i = combo_box->GetCurSel();
	if(i == 0)
	{
		CEdit* my_edit = (CEdit*)GetDlgItem(IDC_EDIT_PASSWORD);
		my_edit->SetReadOnly(TRUE);
		GetDlgItem(IDC_BUTTON_RESETUSER)->EnableWindow(FALSE);
	}
	else
	{
		CEdit* my_edit = (CEdit*)GetDlgItem(IDC_EDIT_PASSWORD);
		my_edit->SetReadOnly(FALSE);
		GetDlgItem(IDC_BUTTON_RESETUSER)->EnableWindow(TRUE);
	}
}


void CLoginDlg::OnClose()
{
	ShowWindow(SW_HIDE);
	CDialogEx::OnClose();
}

void CLoginDlg::OnBnClickedButtonResetuser()
{
	if (ModeFlag == 1)
	{
		// TODO 更新账户密码
		CString userName = L"";
		CString password = L"";
		GetDlgItemText(IDC_EDIT_USERNAME, userName);
		GetDlgItemText(IDC_EDIT_PASSWORD, password);
		gSystemMgr.m_sLoginInfo.m_cPassword = password;
		gSystemMgr.m_sLoginInfo.m_cUserName = userName;
		gSystemMgr. m_cLoginConfigIni.SaveConfigIni(L"LoginConfig", L"m_cPassword", password);
		gSystemMgr.m_cLoginConfigIni.SaveConfigIni(L"LoginConfig", L"m_cUserName", userName);
		m_cPasswordCorrect = gSystemMgr.m_sLoginInfo.m_cPassword;
		m_cUserNameCorrect = gSystemMgr.m_sLoginInfo.m_cUserName;
		MessageBox(L"OK", L"提示", MB_ICONINFORMATION);
	}
	else
	{
		MessageBox(L"请先选择管理员登录", L"提示", MB_ICONERROR);
		gSystemLog.DisplayLogAndSave(L"请先选择管理员登录!", LV_ERROR);
	}
}
